package team.kirohuji.repository.jpa.tidings;

import java.util.Date;
import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import team.kirohuji.domain.tidings.Notify;
import team.kirohuji.domain.tidings.UserNotify;

@Repository
public interface NotifyRepository extends JpaRepository<Notify, Integer> {
	@Query(value = "select *, ABS(NOW() - created_at)  as lastTime from notify where target_type ='系统公告' order by lastTime asc limit 1", nativeQuery = true)
	public Notify findNotifyByLastTime();

	@Query(value = "select * from notify where target_id = ?1 and target_type = ?2 and action = ?3 and created_at > ?4 ", nativeQuery = true)
	public Notify findOneBySubscriptionParam(Integer targetId, String targetType, String action, Date createdAt);

	@Query(value = "select n.*,u.user_name as sender_name from notify n join user_notify un on un.notify_id=n.notify_id join user u on u.user_id=un.user_id where un.is_read=0 and u.user_id= ?1 and n.type = ?2", nativeQuery = true)
	public List<Notify> findAllByUserId(Integer userId, Integer type);
	
	@Query(value = "select * from notify where type =?1", nativeQuery = true)
	public List<Notify> findAllByType(Integer type);
	
}